package cn.stylefeng.guns.modular.taikang.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.taikang.entity.Trade;
import cn.stylefeng.guns.modular.taikang.mapper.TradeMapper;
import cn.stylefeng.guns.modular.taikang.model.params.TradeParam;
import cn.stylefeng.guns.modular.taikang.model.result.TradeResult;
import  cn.stylefeng.guns.modular.taikang.service.TradeService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 行业信息表 服务实现类
 * </p>
 *
 * @author chenmeng
 * @since 2020-11-07
 */
@Service
public class TradeServiceImpl extends ServiceImpl<TradeMapper, Trade> implements TradeService {

    @Override
    public void add(TradeParam param){
        Trade entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(TradeParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(TradeParam param){
        Trade oldEntity = getOldEntity(param);
        Trade newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public TradeResult findBySpec(TradeParam param){
        return null;
    }

    @Override
    public List<TradeResult> findListBySpec(TradeParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(TradeParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(TradeParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Trade getOldEntity(TradeParam param) {
        return this.getById(getKey(param));
    }

    private Trade getEntity(TradeParam param) {
        Trade entity = new Trade();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
